/*
*    Phantom Test Driver v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/*
 * DeclBase.java
 *
 * Created on August 3, 2007, 5:14 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package ptd_decl_objects;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import ssi_utilities.*;

import java.util.Vector;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import ptd.Main;

/**
 *
 * @author John
 */
public class DeclBase extends Object implements Transferable{
    
    public String myName;
    public String myTag;
    public String myClass;
    public int myChild;
    public int myParent;
    public boolean isUseable;
    public int myType;
    
    public boolean bModifiedFlag;
    
    public static int TYPE_TOP = 1;
    public static int TYPE_FILE = 2;
    public static int TYPE_WIN = 3;
    
    

    /** Creates a new instance of DeclBase */
    public DeclBase() {
         isUseable = true;
         myType = TYPE_WIN;
         bModifiedFlag = false;
         myName = "";
         myClass = "";
         myTag = "";
    }
    
    public String parseName(String In){
        String Ret = "";
        int iStart = In.indexOf('"');
        int iEnd = In.lastIndexOf('"');
        if(iStart == iEnd){
            return null;
        }
        Ret = In.substring(iStart+1, iEnd);
        Ret = Ret.trim();
        return Ret;
    }
    
    public String parseProperty(String In){
        String Ret = "";
        int iStart = In.indexOf('=');
        if(iStart < 0){
            return null;
        }
        Ret = In.substring(iStart+1);
        Ret = Ret.trim();
        return Ret;
    }
    
    public String readLine(BufferedReader r){
        String Ret = "";
        String S = Ret;
        while(Ret.length() == 0){
            try {
                S = r.readLine();
                Ret = S;
            } catch (IOException ex) {
                Main.debug_out(ex.getMessage());
            }
            
            if(S == null){
                return null;
            }
            // Do not want to trim Ret, else will remove indentation!
            S = S.trim();
        }
        return Ret;
    }
                
    protected int do_load(BufferedReader r){
        int iLevel = 0;
        try{
            String sName = readLine(r);
            if(sName == null) return 0;
            String sTag = readLine(r);
            if(sTag == null) return 0;
            String sClass = readLine(r);
            if(sClass == null) return 0;
            String sParent = readLine(r);
            if(sParent == null) return 0;            
            String sChild = readLine(r);
            if(sChild == null) return 0;
            
            iLevel = sName.lastIndexOf('\t');
            if(iLevel < 0) iLevel = -1;
            iLevel = iLevel + 2;   // Levels are 1 based, so if it is found at 0 (it is found), so it is already level 2
            
            sName = sName.trim();
            sTag = sTag.trim();
            sClass = sClass.trim();
            sParent = sParent.trim();
            sChild = sChild.trim();
            
            myName = parseName(sName);
            myTag = parseProperty(sTag);
            myClass = parseProperty(sClass);
            sParent = parseProperty(sParent);
            sChild = parseProperty(sChild);
            
            if(myName == null){
                //myName = parseProperty(sClass);
                myName = parseName(sClass);
                isUseable = false;
            }
            
            //if(myName.length() == 0){
            //}
            
            myChild = ssi_converter.IntFromString(sChild);
            myParent = ssi_converter.IntFromString(sParent);            
            
        }catch (Exception e){
            Main.debug_out(e.getMessage());
            return 0;
        }
        
        return iLevel;
    }
    

    protected int do_save(BufferedWriter w, int iLevel){
        String sPref = "";
        String sWrite = "";
        String nl = System.getProperty("line.separator");
        for(int i = 0; i < iLevel; i++){
            sPref = sPref + "\t";
        }
        try{
            
            if(myClass.compareTo("\"Menu\"") == 0){
                if(isUseable){
                    sWrite = sPref + "[ ]Menu \"" + myName + "\"" + nl;
                }else{
                    // This is the dreaded top-menu
                    sWrite = sPref + "[ ]Menu" + nl;
                }
            }else if(myClass.compareTo("Menu") == 0){                
                // This is the dreaded top-menu
                sWrite = sPref + "[ ]Menu" + nl;
            }else if(myClass.compareTo("\"MenuItem\"") == 0){
                sWrite = sPref + "[ ]MenuItem \"" + myName + "\"" + nl;
               
            }else{
                sWrite = sPref + "[ ]MainWin \"" + myName + "\"" + nl;
                //sWrite = sPref + "[ ]" + myClass + " \"" + myName + "\"" + nl;
            }
            
            w.write(sWrite);
            
            sPref = sPref + "\t";
            
            sWrite = sPref + "( )Tag="+ myTag + nl;
            w.write(sWrite);
            sWrite = sPref + "( )Class="+ myClass + nl;
            w.write(sWrite);
            sWrite = sPref + "( )Parent="+ myParent + nl;
            w.write(sWrite);
            sWrite = sPref + "( )Child="+ myChild + nl;
            w.write(sWrite);            
            
        }catch (Exception e){
            Main.debug_out(e.getMessage());
            return 0;
        }
        bModifiedFlag = false;
        return iLevel;
    }
    
    
    public int load(BufferedReader r){
        return do_load(r);
    }
    
    public int save(BufferedWriter w, int iLevel){
        return do_save(w, iLevel);
    }

    
    public Object getTransferData(DataFlavor df) throws UnsupportedFlavorException, IOException {
        if (df.equals( DataFlavor.getTextPlainUnicodeFlavor() ) == false){
            throw new UnsupportedFlavorException(df);
        }
        return myName;
    }
    public DataFlavor[] getTransferDataFlavors() {
       DataFlavor Flavors[] = {DataFlavor.getTextPlainUnicodeFlavor()};
       return Flavors;
    }
    
    //Returns whether or not the specified data flavor is supported for this object.
    public boolean isDataFlavorSupported(DataFlavor flavor) {
       if (flavor == DataFlavor.getTextPlainUnicodeFlavor()){
           return true;
       }
      return false;
    }
    
    public DeclBase duplicate(){
        DeclBase Ret = new DeclBase();
        Ret.isUseable = isUseable;
        Ret.myChild = myChild;
        Ret.myClass = myClass;
        Ret.myName = myName;
        Ret.myParent = myParent;
        Ret.myTag = myTag;
        Ret.myType = myType;
        return Ret;
    }
    
    public boolean isEqual(DeclBase In){
        if(myTag.compareTo(In.myTag) != 0){
            return false;
        }
        if(myName.compareTo(In.myName) != 0){
            return false;
        }
        if(myClass.compareTo(In.myClass) != 0){
            return false;
        }
        return true;
    }
    
}
